package hlhj.fhp.checkpoint.fgms

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.example.mymvp.log
import com.example.mymvp.mvp.BaseFgm
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import hlhj.fhp.checkpoint.R
import hlhj.fhp.checkpoint.adp.ViolationOrderListAdp
import hlhj.fhp.checkpoint.adp.YearOrderListAdp
import hlhj.fhp.checkpoint.atys.OrderDetailAty
import hlhj.fhp.checkpoint.atys.PayAty
import hlhj.fhp.checkpoint.iview.OrderListView
import hlhj.fhp.checkpoint.javaBean.AllBuyEvent
import hlhj.fhp.checkpoint.javaBean.ViolationOrderBean
import hlhj.fhp.checkpoint.javaBean.YearOrderBean
import hlhj.fhp.checkpoint.presenter.OrderListPresenter
import hlhj.fhp.checkpoint.utils.ToastUtils
import kotlinx.android.synthetic.main.lo_publist.view.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.*

/**
 * Created by Never Fear   on 2018\11\12 0012.
Never More....
 */
class OrderListFgm :BaseFgm<OrderListView,OrderListPresenter>(), OrderListView, SpringView.OnFreshListener {
    private var yearOrderListAdp: YearOrderListAdp?=null
    private var violationListAdp:ViolationOrderListAdp?=null
    private val yearDatas=ArrayList<YearOrderBean.InfoBean.ListBean.DataBean>()
    private val violationDatas=ArrayList<ViolationOrderBean.InfoBean.ListBean.DataBean>()
    private val timer=Timer()
    private var timeTask:TimerTask?=null
    var title:String?=arguments?.getString("title")
    private var type=0
    private var tag=1 //1年检 2违章
    private var page=1

    private var oid=""
    private var money=""
    private var offer=""
    companion object {
        fun getInstance(t:String,type:Int,tag:Int): OrderListFgm {
            val listFgm = OrderListFgm()
            val bundle = Bundle()
            bundle.putString("title",t)
            bundle.putInt("type",type)
            bundle.putInt("tag",tag)
            listFgm.arguments=bundle

            return listFgm
        }
    }
    override fun initView() {
        EventBus.getDefault().register(this)
        //年检
        yearOrderListAdp = YearOrderListAdp(yearDatas)
        //违章
        violationListAdp= ViolationOrderListAdp(violationDatas)
        rootView.listView.layoutManager=LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        when(tag){
            1->{
                rootView.listView.adapter= yearOrderListAdp
                timeTask=object :TimerTask(){
                    override fun run() {
                        rootView.post {
                            yearOrderListAdp?.notifyDataSetChanged()
                        }
                    }
                }
                timer.schedule(timeTask,1000,1000)
            }
            2->{
                rootView.listView.adapter= violationListAdp

            }
        }
        yearOrderListAdp?.setEmptyView(R.layout.empty_view,rootView.listView)
        violationListAdp?.setEmptyView(R.layout.empty_view,rootView.listView)

        rootView.spView.header=DefaultHeader(context)
        rootView.spView.footer=DefaultFooter(context)
    }

    @SuppressLint("MissingSuperCall")
    override fun onDestroy() {
        timer.cancel()
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
    override fun logicStart() {
        when(tag){
            1->{
                presenter?.getYear(type,page,context!!)
            }
            2->{
                presenter?.getViolation(type,page,context!!)
            }
        }
    }

    private val GOTODETAIL: Int=11111

    private val GOPAY: Int=11112

    override fun setListener() {
        when(tag){
            1->{
                yearOrderListAdp?.setOnItemClickListener { adapter, view, position ->
                    val intent = Intent(context, OrderDetailAty::class.java)
                    intent.putExtra("id",yearDatas[position].id)
                    startActivityForResult(intent,GOTODETAIL)
                }
            }
            2->{
                violationListAdp?.listener=object :ViolationOrderListAdp.OnPayClick{
                    override fun click(id: Int, money: String, offer: String) {
                        val intent = Intent(context, PayAty::class.java)
                        intent.putExtra("id",id.toString())
                        intent.putExtra("money",money)
                        intent.putExtra("orderType",0)
                        intent.putExtra("offer",offer)
                        startActivityForResult(intent,GOPAY)
                    }
                }
            }
        }

        rootView.spView.setListener(this)
    }

    override fun onLoadmore() {
        page++
        when(tag){
            1->{
                presenter?.getYear(type,page,context!!)
            }
            2->{
                presenter?.getViolation(type,page,context!!)
            }
        }
    }

    override fun onRefresh() {
        page=1
        when(tag){
            1->{
                presenter?.getYear(type,page,context!!)
            }
            2->{
                presenter?.getViolation(type,page,context!!)
            }
        }
    }

    override fun setYearOrders(yearOrderBean: YearOrderBean) {
        if (page==1){
            yearDatas.clear()
        }
        yearDatas.addAll(yearOrderBean.info.list.data)
        rootView.spView.onFinishFreshAndLoad()
        yearOrderListAdp?.notifyDataSetChanged()

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        when(requestCode){
            GOTODETAIL->{
                page=1
                presenter?.getYear(type,page,context!!)
            }
            GOPAY->{
                page=1
                presenter?.getViolation(type,page,context!!)
            }
        }
    }
    override fun setViolationOrders(violationOrderBean: ViolationOrderBean) {
        if (page==1){
            violationDatas.clear()
        }
        rootView.spView.onFinishFreshAndLoad()

        violationDatas.addAll(violationOrderBean.info.list.data)
        violationListAdp?.notifyDataSetChanged()
    }
    @Subscribe(threadMode = ThreadMode.POSTING)
    fun allbuy(allBuyEvent: AllBuyEvent){
        if (type==2){
            for (i in 0..violationDatas.size-1){
                log("fhp","就没进来${violationDatas[i].status}")
                if (violationDatas[i].status==2){
                    if (oid.equals("")){
                        oid=violationDatas[i].id.toString()
                        money=violationDatas[i].offer.toString()
                        offer=(violationDatas[i].serviceFee.toDouble()+violationDatas[i].markFee.toDouble()).toString()
                    }else{
                        oid=oid+",${violationDatas[i].id}"
                        money=(money.toDouble()+violationDatas[i].offer.toDouble()).toString()
                        offer=(offer.toDouble()+violationDatas[i].serviceFee.toDouble()+violationDatas[i].markFee.toDouble()).toString()
                    }
                }
            }
            if (oid.equals(""))return

            val intent = Intent(context, PayAty::class.java)
            intent.putExtra("id",oid)
            intent.putExtra("money",money)
            intent.putExtra("offer",offer)
            intent.putExtra("orderType",0)
            startActivityForResult(intent,GOPAY)

            oid=""
            money=""
            offer=""
        }

    }
    override fun bindView(): OrderListView {
        return this
    }

    override fun createPresenter(): OrderListPresenter {
        return OrderListPresenter()
    }

    override fun doBeforInit() {
        title=arguments!!.getString("title")
        type=arguments?.getInt("type")!!
        tag=arguments?.getInt("tag")!!

    }

    override fun getContentId(): Int {
        return R.layout.lo_publist
    }
}